package com.ldu.exam.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ldu.exam.dao.AccountRepository;
import com.ldu.exam.dao.ClaRepository;
import com.ldu.exam.dao.NoticeRepository;
import com.ldu.exam.dao.SubmitPaperRepository;
import com.ldu.exam.dao.SubmitQuestionRepository;
import com.ldu.exam.model.AccountModel;
import com.ldu.exam.model.ClaModel;
import com.ldu.exam.model.NoticeModel;
import com.ldu.exam.model.SubmitPaper;
import com.ldu.exam.model.SubmitQuestion;
import com.ldu.exam.model.TestQuestionModel;

@Service
public class SubmitService {

	@Autowired SubmitPaperRepository submitPaperRepository;
	@Autowired SubmitQuestionRepository submitQuestionRepository;
	@Autowired AccountRepository accountRepository;
	@Autowired NoticeRepository noticeRepository;
	@Autowired ClaRepository claRepository;
	
	public Page<SubmitPaper> findAll(String name,String accountId,String teacherId,Pageable page)
	{
//		AccountModel teacherAm = this.accountRepository.findById(teacherId).orElse(null);
//		String claId = "";
		
		Page<SubmitPaper> pageList = this.findByCondition(name, accountId, page);
//		if(!StringUtils.isEmpty(teacherAm.getCla()))
//		{
//			claId = teacherAm.getCla().getId();
//			for (int i = pageList.getContent().size() - 1; i >= 0 ; i--) 
//			{
//				AccountModel studentAm  = this.accountRepository.findById(pageList.getContent().get(i).getAccountId()).orElse(null);
//				
//				if(!StringUtils.isEmpty(studentAm) && !StringUtils.isEmpty(studentAm.getCla()) && !studentAm.getCla().getId().equals(claId))
//				{
//					pageList.getContent().remove(pageList.getContent().get(i));
//				}
//			}
//		}
		System.out.println("size:   "+pageList.getContent().size());
		for (SubmitPaper submitPaper : pageList.getContent()) {
			if(!StringUtils.isEmpty(submitPaper.getAccountId()))
			{
				AccountModel am = this.accountRepository.findById(submitPaper.getAccountId()).orElse(null);
				if(am != null);
				submitPaper.setAccountName(am.getNickname());
			}
		}
		return pageList;
	}
	
	private Page<SubmitPaper> findByCondition(String name,String accountId,Pageable page)
	{
		return this.submitPaperRepository.findAll( new Specification<SubmitPaper>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<SubmitPaper> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				List<Predicate> list = new ArrayList<Predicate>();
				if(!StringUtils.isEmpty(name))
				list.add(builder.like( root.<String>get("paperName"), "%"+name+"%"));
				if(!StringUtils.isEmpty(accountId))
				list.add(builder.equal( root.<String>get("accountId"), accountId));
				Predicate[] pd=list.toArray( new Predicate[list.size()]);
				return builder.and(pd);
			}
		},page);
	}
	
	public Object submit(String accountId,String paperName,String classId,Set<TestQuestionModel> set)
	{
		AccountModel am = this.accountRepository.findById(accountId).orElse(null);
		ClaModel claModel = this.claRepository.findById(classId).orElse(null);
		NoticeModel nm = new NoticeModel();
		nm.setTitle("批卷通知");
		nm.setContent("您所分管的班级<"+claModel.getClaName()+">的<"+am.getNickname()+">同学的试卷已提交,请进行批卷!");
		nm.setIsRead("0");
		nm.setAccountId(claModel.getAccount() == null? "" : claModel.getAccount().getId());
		SubmitPaper submitPaper = new SubmitPaper();
		Set<SubmitQuestion> set1 = new HashSet<SubmitQuestion>();
		for (TestQuestionModel testQuestionModel : set) {
			SubmitQuestion sq = new SubmitQuestion();
			sq.setIsCorrected("0");
			sq.setAnswer(testQuestionModel.getAnswer());
			sq.setSubmitAnswer(testQuestionModel.getSubmitAnswer());
			sq.setScore(testQuestionModel.getQuestionScore());
			sq.setContent(testQuestionModel.getContent());
			sq.setQuestionType(testQuestionModel.getQuestionType());
			set1.add(this.submitQuestionRepository.save(sq));
		}
		submitPaper.setAccountId(accountId);
		submitPaper.setIsCorrected("0");
		submitPaper.setQuestions(set1);
		submitPaper.setScore("0");
		submitPaper.setPaperName(paperName);
		am.setIsExamed("1");
		
		Object o  = this.submitPaperRepository.save(submitPaper);
		if(!StringUtils.isEmpty(o))
		{
			this.noticeRepository.save(nm);
		}
		return o;
	}
}
